package com.ruoyi.goods.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.goods.utils.SnowflakeIdUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.ShippingAddressesMapper;
import com.ruoyi.goods.domain.ShippingAddresses;
import com.ruoyi.goods.service.IShippingAddressesService;

/**
 * 收获地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class ShippingAddressesServiceImpl implements IShippingAddressesService 
{
    @Autowired
    private ShippingAddressesMapper shippingAddressesMapper;

    /**
     * 查询收获地址
     * 
     * @param id 收获地址主键
     * @return 收获地址
     */
    @Override
    public ShippingAddresses selectShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.selectShippingAddressesById(id);
    }

    /**
     * 查询收获地址列表
     * 
     * @param shippingAddresses 收获地址
     * @return 收获地址
     */
    @Override
    public List<ShippingAddresses> selectShippingAddressesList(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.selectShippingAddressesList(shippingAddresses);
    }

    /**
     * 新增收获地址
     * 
     * @param shippingAddresses 收获地址
     * @return 结果
     */
    @Override
    public int insertShippingAddresses(ShippingAddresses shippingAddresses) {
        Long userId = SecurityUtils.getUserId();

        // 设置新地址的 ID 和用户 ID

        shippingAddresses.setUserId(userId);

        // 检查当前用户是否已有默认地址
        ShippingAddresses queryAddress = new ShippingAddresses();

        queryAddress.setIsDefault(1); // 查询默认地址

        List<ShippingAddresses> defaultAddresses = shippingAddressesMapper.selectShippingAddressesList(queryAddress);

        if (!defaultAddresses.isEmpty()) {
            for (ShippingAddresses address : defaultAddresses) {
                address.setIsDefault(0); // 将现有的默认地址更新为非默认
                shippingAddressesMapper.updateShippingAddresses(address);
            }
        }

        // 将新地址设置为默认地址
        shippingAddresses.setIsDefault(1);

        // 插入新地址并返回结果
        return shippingAddressesMapper.insertShippingAddresses(shippingAddresses);
    }





    /**
     * 修改收获地址
     * 
     * @param shippingAddresses 收获地址
     * @return 结果
     */

    @Override
    public int updateShippingAddresses(ShippingAddresses shippingAddresses) {
        Long userId = shippingAddresses.getUserId();
        Integer isDefault = shippingAddresses.getIsDefault();

        // 如果设置为默认地址，需要将其他默认地址更新为非默认状态
        if (Integer.valueOf(1).equals(isDefault)) {
            List<ShippingAddresses> defaultAddresses = shippingAddressesMapper.selectShippingAddressesList(new ShippingAddresses() {{
                setUserId(userId);
                setIsDefault(1); // 只查询默认地址
            }});

            for (ShippingAddresses address : defaultAddresses) {
                address.setIsDefault(0);
                shippingAddressesMapper.updateShippingAddresses(address); // 更新为非默认状态
            }
        }

        // 更新当前地址
        return shippingAddressesMapper.updateShippingAddresses(shippingAddresses);
    }


    /**
     * 批量删除收获地址
     * 
     * @param ids 需要删除的收获地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesByIds(Long[] ids)
    {
        return shippingAddressesMapper.deleteShippingAddressesByIds(ids);
    }

    /**
     * 删除收获地址信息
     * 
     * @param id 收获地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.deleteShippingAddressesById(id);
    }
}
package com.ruoyi.goods.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.goods.utils.SnowflakeIdUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.ShippingAddressesMapper;
import com.ruoyi.goods.domain.ShippingAddresses;
import com.ruoyi.goods.service.IShippingAddressesService;

/**
 * 收获地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class ShippingAddressesServiceImpl implements IShippingAddressesService 
{
    @Autowired
    private ShippingAddressesMapper shippingAddressesMapper;

    /**
     * 查询收获地址
     * 
     * @param id 收获地址主键
     * @return 收获地址
     */
    @Override
    public ShippingAddresses selectShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.selectShippingAddressesById(id);
    }

    /**
     * 查询收获地址列表
     * 
     * @param shippingAddresses 收获地址
     * @return 收获地址
     */
    @Override
    public List<ShippingAddresses> selectShippingAddressesList(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.selectShippingAddressesList(shippingAddresses);
    }

    /**
     * 新增收获地址
     * 
     * @param shippingAddresses 收获地址
     * @return 结果
     */
    @Override
    public int insertShippingAddresses(ShippingAddresses shippingAddresses) {
        Long userId = SecurityUtils.getUserId();

        // 设置新地址的 ID 和用户 ID
        shippingAddresses.setId(new SnowflakeIdUtils(5, 8).nextId());
        shippingAddresses.setUserId(userId);

        // 检查当前用户是否已有默认地址
        ShippingAddresses queryAddress = new ShippingAddresses();
        queryAddress.setUserId(userId);
        queryAddress.setIsDefault(1); // 查询默认地址

        List<ShippingAddresses> defaultAddresses = shippingAddressesMapper.selectShippingAddressesList(queryAddress);

        if (!defaultAddresses.isEmpty()) {
            for (ShippingAddresses address : defaultAddresses) {
                address.setIsDefault(0); // 将现有的默认地址更新为非默认
                shippingAddressesMapper.updateShippingAddresses(address);
            }
        }

        // 将新地址设置为默认地址
        shippingAddresses.setIsDefault(1);

        // 插入新地址并返回结果
        return shippingAddressesMapper.insertShippingAddresses(shippingAddresses);
    }





    /**
     * 修改收获地址
     * 
     * @param shippingAddresses 收获地址
     * @return 结果
     */

    @Override
    public int updateShippingAddresses(ShippingAddresses shippingAddresses) {
        Long userId = shippingAddresses.getUserId();
        Integer isDefault = shippingAddresses.getIsDefault();

        // 如果设置为默认地址，需要将其他默认地址更新为非默认状态
        if (Integer.valueOf(1).equals(isDefault)) {
            List<ShippingAddresses> defaultAddresses = shippingAddressesMapper.selectShippingAddressesList(new ShippingAddresses() {{
                setUserId(userId);
                setIsDefault(1); // 只查询默认地址
            }});

            for (ShippingAddresses address : defaultAddresses) {
                address.setIsDefault(0);
                shippingAddressesMapper.updateShippingAddresses(address); // 更新为非默认状态
            }
        }

        // 更新当前地址
        return shippingAddressesMapper.updateShippingAddresses(shippingAddresses);
    }


    /**
     * 批量删除收获地址
     * 
     * @param ids 需要删除的收获地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesByIds(Long[] ids)
    {
        return shippingAddressesMapper.deleteShippingAddressesByIds(ids);
    }

    /**
     * 删除收获地址信息
     * 
     * @param id 收获地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.deleteShippingAddressesById(id);
    }
}
